/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package cn.cloudbae.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;

import cn.cloudbae.schedule.FirstJob;
import cn.cloudbae.schedule.RestartJob;


@Configuration
public class JobConfig {
	
	//引用zk
    @Autowired
    ZookeeperRegistryCenter zookeeperRegistryCenter;
    
    @Autowired
    JobEventConfiguration jobEventConfiguration;
    
    /**
     * http://elasticjob.io/docs/elastic-job-lite/01-start/dev-guide/
     * 
     * Elastic-Job配置分为3个层级，分别是Core、Type、Root。每个层级使用相似于装饰者模式的方式装配。
     * 1. Core对应JobCoreConfiguration，用于提供作业核心配置信息，如：作业名称、分片总数、CRON表达式等。
     * 2. Type对应JobTypeConfiguration，有3个子类分别对应SIMPLE, DATAFLOW和SCRIPT类型作业，提供3种作业需要的不同配置，如：DATAFLOW类型是否流式处理或SCRIPT类型的命令行等。
     * 3. Root对应JobRootConfiguration，有2个子类分别对应Lite和Cloud部署类型，提供不同部署类型所需的配置，如：Lite类型的是否需要覆盖本地配置或Cloud占用CPU或Memory数量等。
     * 4. 如果在Spring工程中使用，则还需要用JobScheduler封装Root。
     */
    
    @Bean
    public FirstJob firstJob() {
    	return new FirstJob();
    }
    
    @Bean
    public RestartJob restartJob() {
    	return new RestartJob();
    }
    
    @Bean(initMethod = "init")
    public JobScheduler firstJobScheduler() {
    	// 1. 创建job-bean所需的参数
    	String jobName = "firstJob123";
    	String cron = "0 0/1 * * * ? *";  //1分钟执行一次
    	int shardingTotalCount = 3; //分片总数
    	//分片参数，一定要从0开始，个数与shardingTotalCount一致
    	String shardingItemParameters="0=宝马,1=奔驰,2=雅迪"; //分片参数
    	
    	//2. 创建job-bean
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(
        		jobName, cron, shardingTotalCount) //job的名称，定时时间，分片个数
        		.shardingItemParameters(shardingItemParameters) //分片参数
        		.description("这是我的firstJob").build();
        
        //3. 把job-bean封装成simple-job-bean
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(
        		simpleCoreConfig, FirstJob.class.getCanonicalName());
        
        //4. 把simple-job-bean封装成lite-simple-job-bean
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(
        		simpleJobConfig).overwrite(true).build(); //overwrite(true)表示每次启动都覆盖zk注册中心的配置
        
        return new SpringJobScheduler(firstJob(), zookeeperRegistryCenter, simpleJobRootConfig, jobEventConfiguration);
    }
    
    @Bean(initMethod = "init")
    public JobScheduler RestartJobScheduler() {
    	// 1. 创建job-bean所需的参数
    	String jobName = "restartJobXXX";
    	String cron = "0 0/20 * * * ? *";  //每20分钟执行一次
    	int shardingTotalCount = 3; //分片总数
    	//分片参数，一定要从0开始，个数与shardingTotalCount一致
    	String shardingItemParameters="0=宝马,1=奔驰,2=雅迪"; //分片参数
    	
    	//2. 创建job-bean
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(
        		jobName, cron, shardingTotalCount) //job的名称，定时时间，分片个数
        		.shardingItemParameters(shardingItemParameters) //分片参数
        		.description("这是我的restartJob").build();
        
        //3. 把job-bean封装成simple-job-bean
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(
        		simpleCoreConfig, RestartJob.class.getCanonicalName());
        
        //4. 把simple-job-bean封装成lite-simple-job-bean
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(
        		simpleJobConfig).overwrite(true).build(); //overwrite(true)表示每次启动都覆盖zk注册中心的配置
        
        return new SpringJobScheduler(restartJob(), zookeeperRegistryCenter, simpleJobRootConfig, jobEventConfiguration);
    }
}
